#!/bin/sh

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use PostProc codes to project the DOS on molecular orbitals."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x projwfc.x molecularpdos.x"
PSEUDO_LIST="H.pbe-rrkjus.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for gnuplot
GP_COMMAND=`which gnuplot 2>/dev/null`
if [ "$GP_COMMAND" = "" ]; then
        $ECHO
        $ECHO "gnuplot not in PATH"
        $ECHO "Results will not be plotted"
fi
 
# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
DOS_COMMAND="$PARA_PREFIX $BIN_DIR/dos.x $PARA_POSTFIX"
PROJWFC_COMMAND="$PARA_PREFIX $BIN_DIR/projwfc.x $PARA_POSTFIX"
MOLECULARPDOS_COMMAND="$PARA_PREFIX $BIN_DIR/molecularpdos.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as:            $PW_COMMAND"
$ECHO "  running projwfc.x as:       $PROJWFC_COMMAND"
$ECHO "  running molecularpdos.x as: $MOLECULARPDOS_COMMAND"
$ECHO "  running gnuplot as:         $GP_COMMAND"
$ECHO

# semi-distance between H atoms in the H2 molecules considered
# (here they are stretched to artificially lower the antibonding orbitals)
d0=1.1
d1=1.0

# z coordinates of the H2 molecules considered
z0=0.0
z1=15.0

# labels for the two H2 molecules
l0="A"
l1="B"
# label for the total system
ll=${l0}${l1}

# which kpoints to test?
kpline0="Gamma"
kpline1=""
#kpline0="automatic"
#kpline1="2 2 2 0 0 0"

# to spin or not to spin?
nspin=1
#nspin="2, tot_magnetization=0"

# self-consistent calculation for two H2 molecules together
cat <<EOF > H2$ll.scf.in
&CONTROL
  calculation  = "scf",
  prefix       = "H2$ll",
  tstress      = .false.,
  tprnfor      = .false.,
  pseudo_dir = '$PSEUDO_DIR/',
  outdir='$TMP_DIR/'
/
&SYSTEM
  ibrav     = 6, 
  celldm(1) = 15.d0
  celldm(3) = 2.d0
  nat       = 4,
  ntyp      = 1,
  ecutwfc   = 24.0,
  nbnd      = 8
  nspin     = $nspin
/
&ELECTRONS
/
ATOMIC_SPECIES
H  1.00  H_US.van
ATOMIC_POSITIONS {bohr}
H   0.0  -${d0}  ${z0}
H   0.0   ${d0}  ${z0}
H   0.0  -${d1}  ${z1}
H   0.0   ${d1}  ${z1}
K_POINTS ${kpline0}
${kpline1}
EOF
$ECHO "  running the scf calculation for two H2 molecules...\c"
$PW_COMMAND < H2$ll.scf.in > H2$ll.scf.out
check_failure $?
$ECHO " done"

# self-consistent calculation for two H2 molecules separately
cat <<EOF > H2$l0.scf.in
&CONTROL
  calculation  = "scf",
  prefix       = "H2$l0",
  tstress      = .false.,
  tprnfor      = .false.,
  pseudo_dir = '$PSEUDO_DIR/',
  outdir='$TMP_DIR/'
/
&SYSTEM
  ibrav     = 6, 
  celldm(1) = 15.d0
  celldm(3) = 2.d0
  nat       = 2,
  ntyp      = 1,
  ecutwfc   = 24.0,
  nbnd      = 8
  nspin     = $nspin
/
&ELECTRONS
/
ATOMIC_SPECIES
H  1.00  H_US.van
ATOMIC_POSITIONS {bohr}
H   0.0  -${d0}  ${z0}
H   0.0   ${d0}  ${z0}
K_POINTS ${kpline0}
${kpline1}
EOF
$ECHO "  running the scf calculation for the H2 molecule ${l0}...\c"
$PW_COMMAND < H2$l0.scf.in > H2$l0.scf.out
check_failure $?
$ECHO " done"

cat <<EOF > H2$l1.scf.in
&CONTROL
  calculation  = "scf",
  prefix       = "H2$l1",
  tstress      = .false.,
  tprnfor      = .false.,
  pseudo_dir = '$PSEUDO_DIR/',
  outdir='$TMP_DIR/'
/
&SYSTEM
  ibrav     = 6, 
  celldm(1) = 15.d0
  celldm(3) = 2.d0
  nat       = 2,
  ntyp      = 1,
  ecutwfc   = 24.0,
  nbnd      = 8
  nspin     = $nspin
/
&ELECTRONS
/
ATOMIC_SPECIES
H  1.00  H_US.van
ATOMIC_POSITIONS {bohr}
H   0.0  -${d1}  ${z1}
H   0.0   ${d1}  ${z1}
K_POINTS ${kpline0}
${kpline1}
EOF
$ECHO "  running the scf calculation for the H2 molecule ${l1}...\c"
$PW_COMMAND < H2$l1.scf.in > H2$l1.scf.out
check_failure $?
$ECHO " done"

# projwfc calculation for the two H2 molecules together and separately
for prefix in H2$ll H2${l0} H2${l1}; do
cat <<EOF > $prefix.projwfc.in
&PROJWFC
  outdir='$TMP_DIR/'
  prefix       = '$prefix'
  ngauss       = 0
  degauss      = 0.00743
  deltae       = 0.02
/
EOF
$ECHO "  running the projwfc calculation for $prefix...\c"
$PROJWFC_COMMAND < $prefix.projwfc.in > $prefix.projwfc.out
check_failure $?
$ECHO " done"
$ECHO " copying atomic_proj.xml file to $prefix.atomic_proj.xml"
cp -p $TMP_DIR/$prefix.save/atomic_proj.xml $prefix.atomic_proj.xml
done

# First/last atomic wavefunctions to consider
# when projecting on the first H2 molecule
atmwfc_b0=1
atmwfc_e0=2
# when projecting on the second H2 molecule
atmwfc_b1=3
atmwfc_e1=4

# project the DOS on the molecular orbitals of the first H2 molecule
cat <<EOF > project_${ll}_onto_${l0}.in
&INPUTMOPDOS
xmlfile_full='H2$ll.atomic_proj.xml'
i_atmwfc_beg_full=${atmwfc_b0},
i_atmwfc_end_full=${atmwfc_e0},
i_bnd_beg_full=1,
i_bnd_end_full=8,
xmlfile_part='H2${l0}.atomic_proj.xml'
i_atmwfc_beg_part=1,
i_atmwfc_end_part=2,
i_bnd_beg_part=1,
i_bnd_end_part=2,
fileout ='project_${ll}_onto_${l0}'
ngauss=0,
degauss=0.00743,
DeltaE=0.02,
/
EOF
$ECHO "  running the molecular orbital projection of the DOS for the molecule $l0...\c"
$MOLECULARPDOS_COMMAND < project_${ll}_onto_${l0}.in > project_${ll}_onto_${l0}.out
check_failure $?
$ECHO " done"

# project the DOS on the molecular orbitals of the second H2 molecule
cat <<EOF > project_${ll}_onto_${l1}.in
&INPUTMOPDOS
xmlfile_full='H2$ll.atomic_proj.xml'
i_atmwfc_beg_full=${atmwfc_b1},
i_atmwfc_end_full=${atmwfc_e1},
i_bnd_beg_full=1,
i_bnd_end_full=8,
xmlfile_part='H2${l1}.atomic_proj.xml'
i_atmwfc_beg_part=1,
i_atmwfc_end_part=2,
i_bnd_beg_part=1,
i_bnd_end_part=2,
fileout ='project_${ll}_onto_${l1}'
ngauss=0,
degauss=0.00743,
DeltaE=0.02,
/
EOF
$ECHO "  running the molecular orbital projection of the DOS for the molecule $l1...\c"
$MOLECULARPDOS_COMMAND < project_${ll}_onto_${l1}.in > project_${ll}_onto_${l1}.out
check_failure $?
$ECHO " done"

#
#  if gnuplot was found, the results are plotted
#
if [ "$GP_COMMAND" = "" ]; then
    break
else
cat > gnuplot.tmp <<EOF
#!$GP_COMMAND
#
set term post color solid enh
set out 'project_${ll}_onto_${l0}_${l1}.ps'
set xlabel "Energy (eV)"
set ylabel "DOS (states/eV)"
shift=10.0
set rmargin 20
set key top left
set label 1 "bonding H2 orbital" at graph 1.01, first shift
set label 2 "antibonding H2 orbital" at graph 1.01, first 2*shift
plot 'H2$ll.pdos_tot' t "total DOS for two molecules ($ll)", \
'project_${ll}_onto_${l0}.mopdos' u 2:(\$3+\$1*shift) t "projection on H2-${l0}", \
'project_${ll}_onto_${l1}.mopdos' u 2:(\$3+\$1*shift) t "projection on H2-${l1}"
EOF
$ECHO
$ECHO "  plotting molecular orbital projected DOS ...\c"
$GP_COMMAND < gnuplot.tmp
$ECHO " done"
rm gnuplot.tmp
fi

$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/ni.*

$ECHO
$ECHO "$EXAMPLE_DIR: done"
